﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QL_DTTN.TTKT.BUS;
using QL_DTTN.DTO.DTO;
using DevExpress.XtraGrid.Views.Grid;
namespace QL_DTTN.TTKT
{
    public partial class FrmThemKyThi : DevExpress.XtraEditors.XtraForm
    {

        List<MonThi> dtMonThi;
        List<Mon> dtMon;
        List<ChiTietMonThi> dtChiTietMonThi;
        public FrmThemKyThi()
        {
            InitializeComponent();
            dtMonThi = new List<MonThi>();
            dtMon = new List<Mon>();
            dtChiTietMonThi = new List<ChiTietMonThi>();
        }
        private void FrmThemKyThi_Load(object sender, EventArgs e)
        {
            cbxNam_Load();
            cbxMon_Load();
            cbxHeDaoTao_Load();
            loadControl();
            gctMonThi.DataSource = dtMonThi;
            gctChiTiet.DataSource = dtChiTietMonThi;
        }
        private void btnLuuMonThi_Click(object sender, EventArgs e)
        {
            if (tetThoiGianBatDau.Text.Trim() == "" || tetThoiGianKetThuc.Text.Trim() == "")
            {
                XtraMessageBox.Show("Chưa nhập đầy đủ thông tin!");
                return;
            }
            MonThi monThi = new MonThi();
            Mon mon = new Mon();
            mon.MaMon = Convert.ToInt16(cbxMonHoc.SelectedValue);
            DataRowView i = (DataRowView)cbxMonHoc.SelectedItem;
            mon.TenMon = i["tenMon"].ToString();
            monThi.MaMon = Convert.ToInt16(cbxMonHoc.SelectedValue);
            monThi.ThoiGianBatDau = Convert.ToDateTime(tetThoiGianBatDau.Text);
            monThi.ThoiGianKetThuc = Convert.ToDateTime(tetThoiGianKetThuc.Text);
            cbxMonHoc.SelectedIndex = (cbxMonHoc.SelectedIndex == (cbxMonHoc.Items.Count - 1)) ? 0 : cbxMonHoc.SelectedIndex + 1;
            if (dtMonThi.Where(t => t.MaMon == monThi.MaMon).Count() > 0)
            {
                XtraMessageBox.Show("Thông đã tồn tại!");
                return;
            }
            dtMon.Add(mon);
            dtMonThi.Add(monThi);

            cbxMonThi_Load(dtMon);
            gvwMonThi.RefreshData();
        }
        private void gctMonThi_ProcessGridKey(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete && gvwMonThi.State != DevExpress.XtraGrid.Views.Grid.GridState.Editing)
            {
                if (XtraMessageBox.Show("Bạn muốn xóa?", "Quest", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    Int16 ma = Convert.ToInt16(gvwMonThi.GetRowCellValue(gvwMonThi.FocusedRowHandle, gvwMonThi.Columns["MaMon"]));
                    dtMon.Remove(dtMon.Where(m => m.MaMon == ma).First());
                    var cts = dtChiTietMonThi.Where(c => c.MaMonThi == ma).ToList();
                    if (cts.Count() > 0)
                    {
                        for (int i = 0; i < cts.Count(); i++)
                        {
                            dtChiTietMonThi.Remove(cts[i]);
                        }
                    }
                    dtMonThi.RemoveAt(gvwMonThi.FocusedRowHandle);
                    gvwMonThi.RefreshData();
                    gvwChiTiet.RefreshData();
                    cbxMonThi_Load(dtMon);
                }
            }
        }
        private void gvwMonThi_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            GridView view = sender as GridView;
            MonThi monThi = new MonThi();
            if (view.GetRowCellValue(e.RowHandle, view.Columns["ThoiGianBatDau"]).ToString().Trim()==""||view.GetRowCellValue(e.RowHandle, view.Columns["ThoiGianKetThuc"]).ToString().Trim()=="")
            {
                XtraMessageBox.Show("Có dữ liệu rỗng!");
                return;
            }
            monThi.MaMon = Convert.ToInt16(view.GetRowCellValue(e.RowHandle, view.Columns["MaMon"]));
            monThi.ThoiGianBatDau = Convert.ToDateTime(view.GetRowCellValue(e.RowHandle, view.Columns["ThoiGianBatDau"]));
            monThi.ThoiGianKetThuc = Convert.ToDateTime(view.GetRowCellValue(e.RowHandle, view.Columns["ThoiGianKetThuc"]));
            var mt = dtMonThi.Where(t => t.MaMon == monThi.MaMon);
            if (mt.Count() > 0)
            {
                mt.First().ThoiGianBatDau = monThi.ThoiGianBatDau;
                mt.First().ThoiGianKetThuc = monThi.ThoiGianKetThuc;
                view.RefreshData();
            }
        }
        private void btnLuuChuongTrinh_Click(object sender, EventArgs e)
        {
            ChiTietMonThi ct = new ChiTietMonThi();
            ct.MaMonThi = Convert.ToInt16(cbxMonThi.SelectedValue);
            ct.MaHeDaoTao = Convert.ToInt16(cbxHeDaoTao.SelectedValue);
            if (cbxMonThi.SelectedIndex == cbxMonThi.Items.Count - 1)
            {
                cbxHeDaoTao.SelectedIndex = (cbxHeDaoTao.SelectedIndex == (cbxHeDaoTao.Items.Count - 1)) ? 0 : cbxHeDaoTao.SelectedIndex + 1;
            }
            cbxMonThi.SelectedIndex = (cbxMonThi.SelectedIndex == (cbxMonThi.Items.Count - 1)) ? 0 : cbxMonThi.SelectedIndex + 1;
            if (dtChiTietMonThi.Where(c => c.MaMonThi == ct.MaMonThi && c.MaHeDaoTao == ct.MaHeDaoTao).Count() > 0)
            {
                XtraMessageBox.Show("Thông đã tồn tại!");
                return;
            }
            dtChiTietMonThi.Add(ct);
            gvwChiTiet.RefreshData();
        }
        private void gctChiTiet_ProcessGridKey(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete && gvwMonThi.State != DevExpress.XtraGrid.Views.Grid.GridState.Editing)
            {
                if (XtraMessageBox.Show("Bạn muốn xóa?", "Quest", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    dtChiTietMonThi.RemoveAt(gvwChiTiet.FocusedRowHandle);
                    gvwChiTiet.RefreshData();
                }
            }
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (cbxNamThi.Text.Trim() == "" || txtTenKyThi.Text.Trim() == "")
            {
                XtraMessageBox.Show("Chưa nhập thông tin kỳ thi!");
                return;
            }
            if (cbxNamThi.Text.Trim().Count() > 5)
            {
                XtraMessageBox.Show("Năm thi phải ít hơn 5 ký tự!");
                return;
            }
            KyThi kyThi = new KyThi();
            kyThi.NamThi = cbxNamThi.Text.ToString();
            kyThi.TenKyThi = txtTenKyThi.Text.Trim();
            if (dtMonThi.Count()<1)
            {
                XtraMessageBox.Show("Chưa nhập môn thi!");
                return;
            }
            if (dtChiTietMonThi.Count()<1)
            {
                XtraMessageBox.Show("Chưa nhập chương trình thi!");
                return;
            }
            string kq = KyThi_Bus.Them(kyThi, dtMonThi, dtChiTietMonThi);
            XtraMessageBox.Show((kq == "") ? "Thành công!" : kq);
        }
        private void cbxNam_Load()
        {
            for (int i = 0; i < 100; i++)
            {
                cbxNamThi.Properties.Items.Add(2000 + i);
            }
        }
        private void cbxMon_Load()
        {
            cbxMonHoc.DataSource = MonHoc_Bus.dataTable();
            cbxMonHoc.DisplayMember = "tenMon";
            cbxMonHoc.ValueMember = "maMon";
        }
        private void cbxMonThi_Load(List<Mon> lstMon)
        {
            cbxMonThi.DataSource = null;
            cbxMonThi.DataSource = lstMon;
            cbxMonThi.DisplayMember = "TenMon";
            cbxMonThi.ValueMember = "MaMon";
        }
        private void cbxHeDaoTao_Load()
        {
            cbxHeDaoTao.DataSource = HeDaoTao_Bus.dataTable();
            cbxHeDaoTao.ValueMember = "maHeDaoTao";
            cbxHeDaoTao.DisplayMember = "tenHeDaoTao";
        }
        private void loadControl()
        {
            DataTable dt1 = MonHoc_Bus.dataTable();
            iluMon.DataSource = dt1;
            iluMon.DisplayMember = "tenMon";
            iluMon.ValueMember = "maMon";
            DataTable dt2 = MonHoc_Bus.dataTable();
            iluMonCT.DataSource = dt2;
            iluMonCT.DisplayMember = "tenMon";
            iluMonCT.ValueMember = "maMon";
            DataTable dt3 = HeDaoTao_Bus.dataTable();
            iluHeDaoTao.DataSource = dt3;
            iluHeDaoTao.DisplayMember = "tenHeDaoTao";
            iluHeDaoTao.ValueMember = "maHeDaoTao";
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult = MessageBox.Show("Bạn có chắc muốn thoát không?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (DialogResult == DialogResult.OK)
            {
                this.Close();
            }
        }

        private void gbxDanhSachMonThi_Enter(object sender, EventArgs e)
        {

        }
    }
}